package flc.ast.activity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PicAlbumAdapter;
import flc.ast.bean.PicAlbumBean;
import flc.ast.databinding.ActivityAlbumBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class AlbumActivity extends BaseAc<ActivityAlbumBinding> {

    public static String sFilePath;
    private PicAlbumAdapter mPicAlbumAdapter;
    private List<PicAlbumBean> listShow = new ArrayList<>();
    private List<String> listSel = new ArrayList<>();
    private boolean isEdit;

    @Override
    protected int onCreate() {
        return R.layout.activity_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAlbumBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                if (isEdit) {
                    isEdit = false;
                    listSel.clear();
                    mDataBinding.tvBatchDelete.setVisibility(View.VISIBLE);
                    mDataBinding.tvAllDelete.setVisibility(View.GONE);
                    mDataBinding.ivAdd.setVisibility(View.VISIBLE);
                    mDataBinding.tvDelete.setVisibility(View.GONE);
                    getData();
                } else {
                    finish();
                }
            }
        });

        mDataBinding.tvBatchDelete.setOnClickListener(this);
        mDataBinding.tvAllDelete.setOnClickListener(this);
        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mPicAlbumAdapter = new PicAlbumAdapter();
        mDataBinding.rvPictureList.setAdapter(mPicAlbumAdapter);
        mPicAlbumAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvBatchDelete) {//批量删除
            isEdit = true;
            mDataBinding.tvBatchDelete.setVisibility(View.GONE);
            mDataBinding.tvAllDelete.setVisibility(View.VISIBLE);
            mDataBinding.ivAdd.setVisibility(View.GONE);
            mDataBinding.tvDelete.setVisibility(View.VISIBLE);
            mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
        } else if (id == R.id.tvAllDelete) {//全选删除
            listSel.clear();
            for (PicAlbumBean picAlbumBean : listShow) {
                picAlbumBean.setSelected(true);
                listSel.add(picAlbumBean.getPath());
            }
            mPicAlbumAdapter.notifyDataSetChanged();
            mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
        } else if (id == R.id.ivAdd) {//添加图片
            startActivity(AddPictureActivity.class);
        } else if (id == R.id.tvDelete) {//删除
            delete();
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (mPicAlbumAdapter.getItem(position).isSelected()) {
                mPicAlbumAdapter.getItem(position).setSelected(false);
                listSel.remove(mPicAlbumAdapter.getItem(position).getPath());
            } else {
                mPicAlbumAdapter.getItem(position).setSelected(true);
                listSel.add(mPicAlbumAdapter.getItem(position).getPath());
            }
            mPicAlbumAdapter.notifyItemChanged(position);
            mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvAlbumTitle.setText(sFilePath);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myAlbum + "/" + sFilePath);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new PicAlbumBean(file.getPath()));
            }
        }
        if (listShow.size() > 0) {
            mPicAlbumAdapter.setList(listShow);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.tvPictureNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除
    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {
                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        for (int i = 0; i < listSel.size(); i++) {
                            FileUtils.delete(listSel.get(i));
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        listSel.clear();
                        mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
                        getData();
                    }
                });
            }
        }, 500);
    }

}